using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Systems.Security;
using Deepcode.Flux.Core.Systems.Registry;
using Deepcode.Flux.Core.Systems.Audit.Quick;

public partial class Private_Config_Registry_report : Deepcode.Flux.Core.UI.AdministrationModulePage
{
	#region Permission Code Override
	public override string RequiredPermissionCode { get { return "FluxRegistry"; } }
	#endregion
	#region ViewState properties
	/// <summary>
	/// Store the registry path being shown
	/// </summary>
	private int RegistryPath
	{
		get
		{
			object o = ViewState["rpath"];
			if (o == null) return -1;
			return (int) o;
		}
		set
		{
			ViewState["rpath"] = value;
		}
	}
	#endregion
	#region Page Load Event
	/// <summary>
	/// Page Load event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			RegistryPath = GetAttribute("rpath", -1);
			BindReport();
		}
	}
	#endregion
	#region Init
	/// <summary>
	/// Overridden on init for capturing events
	/// </summary>
	/// <param name="e"></param>
	protected override void OnInit(EventArgs e)
	{
		base.OnInit(e);
		_paths.ItemCommand += new DataGridCommandEventHandler(_paths_ItemCommand);
		_data.ItemCommand += new DataGridCommandEventHandler(_data_ItemCommand);
		btnAddNew.Click += new EventHandler(btnAddNew_Click);
		btnAddNewPath.Click += new EventHandler(btnAddNewPath_Click);
	}
	#endregion
	#region Data Binding
	/// <summary>
	/// Query the event log and display in the grid
	/// </summary>
	private void BindReport()
	{
		RegistryDataManager rdm = new RegistryDataManager();
		// Get and set the information for the path label
		RegistryDataNode trav = rdm.GetByID( this.RegistryPath );
		if( trav == null ) txtCurrentPath.Text = "/";
		else txtCurrentPath.Text = "";

		if (trav != null)
		{
			while (trav != null)
			{
				txtCurrentPath.Text = String.Format("{0}/{1}", trav.KeyName, txtCurrentPath.Text);
				trav = trav.Parent;
			}

			txtCurrentPath.Text = String.Format("/{0}", txtCurrentPath.Text);
		}

		// Get, Seperate and Populate the Grids
		List<RegistryDataNode> DataNodes = new List<RegistryDataNode>();
		List< RegistryDataNode > PathNodes = new List<RegistryDataNode>();

		foreach (RegistryDataNode node in rdm.GetChildrenFor(this.RegistryPath))
		{
			if( ((FluxRegistryKeyType) node.KeyType) == FluxRegistryKeyType.NODEPATH )
				PathNodes.Add( node );
			else
				DataNodes.Add( node );
		}

		_data.DataSource = DataNodes;
		_data.DataBind();
		
		_paths.DataSource = PathNodes;
		_paths.DataBind();
	}
	#endregion
	#region User Interactions

	/// <summary>
	/// Action occurred on the paths grid
	/// </summary>
	/// <param name="source"></param>
	/// <param name="e"></param>
	void _paths_ItemCommand(object source, DataGridCommandEventArgs e)
	{
		if (e.Item.ItemType == ListItemType.Footer ||
			e.Item.ItemType == ListItemType.Header ||
			e.Item.ItemType == ListItemType.Pager ||
			e.Item.ItemType == ListItemType.Separator) return;

		// Get the key of the row being actioned 
		int rowKey = (int) _paths.DataKeys[e.Item.ItemIndex];

		// PROCESS COMMANDS
		if (e.CommandName == "Delete")
		{
			RegistryDataManager rdm = new RegistryDataManager();
			RegistryDataNode node = rdm.GetByID(rowKey);
			if (node != null)
			{
				rdm.DeleteByID(rowKey);

				QuickAudit.RaiseImportant(
					new AuditUser(),
					"Flux", "Registry Editor",
					"deleted path {0}{1}",
					this.txtCurrentPath.Text,
					node.KeyName);
			}

			this.BindReport();
			return;
		}
		if (e.CommandName == "Edit")
		{
			string url = String.Format("ui_FormPath.aspx?ID={0}&ParentID={1}", rowKey, this.RegistryPath);
			Response.Redirect(url, true);
			return;
		}
		if (e.CommandName == "Nav")
		{
			string url = String.Format("report.aspx?RPath={0}", rowKey);
			Response.Redirect(url, true);
			return;
		}
	}
	/// <summary>
	/// Action occurred on the nodes/data grid
	/// </summary>
	/// <param name="source"></param>
	/// <param name="e"></param>
	void _data_ItemCommand(object source, DataGridCommandEventArgs e)
	{
		if (e.Item.ItemType == ListItemType.Footer ||
			e.Item.ItemType == ListItemType.Header ||
			e.Item.ItemType == ListItemType.Pager ||
			e.Item.ItemType == ListItemType.Separator) return;

		// Get the key of the row being actioned 
		int rowKey = (int)_data.DataKeys[e.Item.ItemIndex];

		// PROCESS COMMANDS
		if (e.CommandName == "Delete")
		{
			RegistryDataManager rdm = new RegistryDataManager();
			RegistryDataNode node = rdm.GetByID( rowKey );
			if (node != null)
			{
				rdm.DeleteByID(rowKey);

				QuickAudit.RaiseImportant(
					new AuditUser(),
					"Flux", "Registry Editor",
					"deleted key {0}{1}",
					this.txtCurrentPath.Text,
					node.KeyName);
			}

			this.BindReport();
			return;
		}
		if (e.CommandName == "Edit")
		{
			string url = String.Format("ui_FormNode.aspx?ID={0}&ParentID={1}", rowKey, this.RegistryPath);
			Response.Redirect(url, true);
			return;
		}
	}

	/// <summary>
	/// Add new path button clicked
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	void btnAddNewPath_Click(object sender, EventArgs e)
	{
		Response.Redirect( 
			String.Format("ui_FormPath.aspx?ParentID={0}", this.RegistryPath),
			true );
	}

	/// <summary>
	/// Add new node button clicked
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	void btnAddNew_Click(object sender, EventArgs e)
	{
		Response.Redirect(
			String.Format("ui_FormNode.aspx?ParentID={0}", this.RegistryPath),
			true);
	}
	#endregion
}
